package com.ysm.smfu.activity;

import android.app.ProgressDialog;
import android.content.SharedPreferences;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.adapter.BaseListAdapter;
import com.ysm.smfu.bean.HomeSearchResultBean;
import com.ysm.smfu.contans.SpKey;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.utils.UIUtils;
import com.ysm.smfu.view.RefreshLayout;
import com.ysm.smfu.welcome.SharedConfig;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/8.
 */
public class CategoryActivity extends BaseRefreshActivity<HomeSearchResultBean.BusinessInfo> {
    @BindView(R.id.act_category_listView)
    ListView actCategoryListView;

    @BindView(R.id.refresh)
    RefreshLayout refreshLayout;

    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView topbarimg;
    @BindView(R.id.only_no)
    ImageView only_no;
    @BindView(R.id.listInfo)
    AutoLinearLayout listInfo;
    private String typeId = "";
    private String cityId = "";
    private String currCityId = "";
    private String lat = "";
    private String lon = "";
    private BaseListAdapter adapter;
    private ProgressDialog pb;
    private String type = "";

    @Override
    protected void init() {
        super.init();
        typeId = getIntent().getStringExtra("typeId");
        type = getIntent().getStringExtra("type");
    }

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_categoty);
        ButterKnife.bind(this);
        initRefresh(refreshLayout, true);
        titleBody.setText(type);
        topbarimg.setVisibility(View.GONE);
        //      搜索后刷新
        adapter = new BaseListAdapter(datas, actCategoryListView, this);
        actCategoryListView.setAdapter(adapter);
        getConfig();
    }

    /**
     * 获取配置信息
     */
    private void getConfig() {
        showDialog();
        SharedPreferences sp = new SharedConfig(UIUtils.getContext()).getCommoneSp();
        cityId = sp.getString(SpKey.SELECID, "");
        currCityId = sp.getString(SpKey.LOCID, "");
        lat = sp.getString(SpKey.LAT, "");
        lon = sp.getString(SpKey.LON, "");
        searchByCategory();
    }

    /**
     * 根据不同分类搜索
     */
    private void searchByCategory() {
        BaseProtocal.builder(false)
                .getCategoryInfo(new MyStringCallBack() {
                    @Override
                    public void onSuccess(String response) {
                        PrintUtil.printRespones("获取分类商家", response);
                        String state = JsonUtil.getState(response);
                        if (TextUtils.equals(state, "1")) {

                            HomeSearchResultBean homeSearchResultBean = new Gson().fromJson(response, HomeSearchResultBean.class);
                            List<HomeSearchResultBean.BusinessInfo> currBiList = homeSearchResultBean.biList;

                            setLoadData(currBiList);

                            adapter.notifyDataSetChanged();

                            if (datas.size() > 0) {
                                listInfo.setVisibility(View.VISIBLE);
                                only_no.setVisibility(View.GONE);
                            } else {
                                listInfo.setVisibility(View.GONE);
                                only_no.setVisibility(View.VISIBLE);
                            }
                            cancelDialog();
                        } else {
                            noData();
                        }
                    }

                    @Override
                    public void onFail(Exception e) {
                        noData();
                    }
                }, page, lon, lat, cityId, currCityId, typeId);
    }

    private void noData() {
        onRefreshError();
        if (datas.size() == 0) {
            listInfo.setVisibility(View.GONE);
            only_no.setVisibility(View.VISIBLE);
        }
        cancelDialog();
    }

    public void showDialog() {
        pb = new ProgressDialog(this);
        pb.setMessage("商品正在赶来的路上，请稍等...");
        pb.show();
    }

    public void cancelDialog() {
        if (pb != null && pb.isShowing())
            pb.dismiss();
    }


    @Override
    public void load(boolean isLoad) {
        if (isLoad)
            page++;
        searchByCategory();
    }
}
